﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.WebControls;
using NetDon.eBoss.Data.CollectionClasses;
using NetDon.eBoss.Data.EntityClasses;
using NetDon.eBoss.Data.HelperClasses;
using NetDon.eBoss.Portal.Common;
using NetDon.eBoss.Portal.HtmlTool;

namespace NetDon.OA.Portal.HtmlTool
{
    public partial class eBayColorManagement : NetDonPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                Bind();

            }
        }


        void Bind()
        {
            gridCustomerInfoList.DataSource = new HtmlToolService().GetColorSettings();
            gridCustomerInfoList.DataBind();
        }

        protected void btnReload_Click(object sender, EventArgs e)
        {
            Bind();
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnDeleteDemand_Click(object sender, EventArgs e)
        {

            var trans = new Transaction(IsolationLevel.ReadCommitted, "btnDeleteColors");
            try
            {
                var batchId = fieldCustomerId.Value;

                var colors = new HtmltoolcolorCollection();
                colors.GetMulti(HtmltoolcolorFields.Batchid == batchId);
                var defaultSiteColor = colors.FirstOrDefault(style => style.Sitecode == (int)eBaySites.US);
                if (defaultSiteColor == null) return;
                //check if used by product description
                var collection = new TitlesettingCollection();
                collection.GetMulti(TitlesettingFields.Htmltoolcolorid == defaultSiteColor.Id);
                if (collection.Count > 0)
                {
                    Alert("不可以删除。标题颜色设定正在使用该eBay颜色");
                    return;
                }

                foreach (var style in colors)
                {
                    style.Delete();
                    trans.Add(style);
                }
                trans.Commit();

                Bind();
                Alert("删除成功!");
            }
            catch (Exception)
            {
                trans.Rollback();
                Alert("删除失败！");
                throw;
            }


        }
        protected void gridCustomerInfoList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (e.Row.DataItem);

                var batchId = e.Row.Attributes["BatchId"] = Convert.ToString(DataBinder.Eval(entity, "BatchId"));


            }
        }


    }
}